package com.chengdu.chun.controller;

import com.chengdu.chun.po.Classes;
import com.chengdu.chun.po.Page;
import com.chengdu.chun.po.User;
import com.chengdu.chun.service.ClassesService;
import com.chengdu.chun.util.Sys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/classes")
public class ClassesController {

    @Autowired
    private HttpSession session;

    @Autowired
    private ClassesService classesService;

    /**
     * 带分页处理的班级列表
     *
     * @param page
     * @param number
     * @return
     */
    @GetMapping("/classeslistbyteacher/{page}/{number}.html")
    public ModelAndView classeslistbyteacher(@PathVariable("page") Integer page, @PathVariable("number") Integer number) {
        if (judguUser()) return new ModelAndView("redirect:/login.html");
        if (page == null || page <= 0) page = 1;
        if (number == null || number <= 0) number = Sys.PAGE_THREE;
        Page<Classes> classesPage = classesService.getClassesByTeacherAndPage(
                ((User) session.getAttribute("user")).getUid(),
                page, number);
        ModelAndView mv = new ModelAndView();
        mv.addObject("classesPage", classesPage);
        mv.setViewName("classesbyteacher");
        return mv;
    }

    @GetMapping("/classesbyname")
    public ModelAndView classesbyname(String classesname) {
        if (judguUser()) return new ModelAndView("redirect:/login.html");
        String cname = classesname.length() == 0 ? null : classesname;
        List<Classes> classesList = classesService.getClassesByTeacherAndName(cname, ((User) session.getAttribute("user")).getUid());
        ModelAndView mv = new ModelAndView();
        Page<Classes> classesPage = new Page<>(1,classesList.size(),classesList.size());
        classesPage.setDatalist(classesList);
        mv.addObject("classesPage", classesPage);
        mv.addObject("cname", cname);
        mv.setViewName("classesbyteacher");
        return mv;
    }

    @GetMapping("/addclassespage")
    public ModelAndView addclassespage() {
        if (judguUser()) return new ModelAndView("redirect:/login.html");
        ModelAndView mv = new ModelAndView("addclasses");
        // 查询当前在职的班主任老师信息列表


        return mv;
    }

    /**
     * 添加新的班级，相关参数值该如何书写
     *
     * @return
     */
    @PostMapping("/addclasses")
    /**
     * 可以使用对象的形式进行请求参数的接收，但是注意：参数名称必须和类的属性同名，且数据类型必须保持一致（或者能被自动转换）
     */
    public ModelAndView addclasses(Classes classes) {
        if (judguUser()) return new ModelAndView("redirect:/login.html");
        ModelAndView mv = new ModelAndView("redirect:/classes/classeslistbyteacher/1/3.html");
        // 调用添加班级的参数
        classes.setUid(((User) session.getAttribute("user")).getUid());
        int num = classesService.insertClasses(classes);
        if (num <= 0) {
            mv.addObject("errinfo", "添加新班级失败！！！");
            mv.setViewName("err");
        }

        return mv;
    }

    /**
     * Ajax进行班级名称的验证，保证班级名称不重复
     * 在注解 RequestMapping 中添加属性produces = "application/json;charset=utf-8"，确保json数据的字符集
     * 另外添加注解ResponseBody，确保可以返回字符串格式的数据（也可以说json格式）
     *
     * @param model
     * @param cname
     * @return
     */
    @RequestMapping(value = "/ajaxchkname", method = RequestMethod.GET,
            produces = "application/json;charset=utf-8")
    @ResponseBody
    public String ajaxCheckClassesName(Model model, String cname) {
        // 调用service层代码根据当前的班级名称去查询班级信息
        boolean bl = classesService.judgeClassesByName(cname);
        if (bl)
            return "{\"info\":\"no\"}";
        else
            return "{\"info\":\"ok\"}";
    }


    private boolean judguUser() {
        if (session.getAttribute("user") == null) {
            return true;
        }
        return false;
    }
}
